package net.sashag.nadlandroid;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class ApartmentAdapter extends ArrayAdapter<Apartment> {
	
	public ApartmentAdapter(Context context, List<Apartment> apartments) {
		super(context, R.layout.apartment_row, apartments);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final Apartment apartment = getItem(position);
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			convertView = inflater.inflate(R.layout.apartment_row, null);
		}
		convertView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				new ApartmentDetailsActivity_.IntentBuilder_(getContext()).apartment(apartment).start();
			}
		});
		ImageButton contactOwner = (ImageButton)convertView.findViewById(R.id.btnContactOwner);
		contactOwner.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				apartment.showContactOptions(getContext());
			}
		});
		TextView txtTop = (TextView)convertView.findViewById(R.id.txtTopLine);
		TextView txtDetails = (TextView)convertView.findViewById(R.id.txtDetails);
		txtTop.setText(apartment.getAddress());
		txtDetails.setText(String.format("Price: %d, bedrooms: %d", apartment.getPrice(), apartment.getBedrooms()));
		return convertView;
	}
}
